package com.serverapk.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;
import android.text.TextUtils;

public class ToolsUtil {

	public static String md5(String string) {
		if (TextUtils.isEmpty(string)) {
			return "";
		}
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] bytes = md5.digest(string.getBytes());
			StringBuilder result = new StringBuilder();
			for (byte b : bytes) {
				String temp = Integer.toHexString(b & 0xff);
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				result.append(temp);
			}
			return result.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static int getUTC() {
		int utc = 0;
		Calendar cal = Calendar.getInstance();
		utc = (int) (cal.getTimeInMillis() / 1000);
		return utc;
	}

	@SuppressLint("SimpleDateFormat")
	public static String getCurrentTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = df.format(new Date());
		return str;
	}

	public static short bytes2Short(byte[] bytes) {
		return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
	}

	public static int bytes2Int(byte[] bytes) {
		int value = 0;
		value = ((bytes[3] & 0xff) << 24) | ((bytes[2] & 0xff) << 16) | ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
		return value;
	}

	public static int getUnsignedByte(byte data) { // 0~255
		return data & 0x0FF;
	}
//
//	public int getUnsignedByte(short data) { //0~65535
//		return data & 0x0FFFF;
//	}
//
//	public long getUnsignedIntt(int data) { //0~4294967295
//		return data & 0x0FFFFFFFF;
//	}

}
